﻿using Soft.Data;
using Soft.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Soft.Service
{

    /// <summary>
    /// 数据库配置分组
    /// </summary>
    public sealed class DBConnectionConfigGroup : BaseSubElement
    {
        public DBConnectionConfigGroup(XmlElement current) : base(current)
        {
        }

       

        /// <summary>
        /// 数据库的类型
        /// </summary>
        public DBType DBType
        {
            get
            {
                return base.CurrentElement.GetAttribute("DBType").ConvertTo(DBType.SqlServer, true);
            }
            set
            {
                base.CurrentElement.SetAttribute("DBType", value.ToString());
            }
        }



        /// <summary>
        /// 方法所属的模块
        /// </summary>
        public MesMethodFlag MethodFlags
        {
            get
            {
                return base.CurrentElement.GetAttribute("MethodFlags").ConvertTo(MesMethodFlag.DEMO, true);
            }
            set
            {
                base.CurrentElement.SetAttribute("MethodFlags", value.ToString());
            }
        }

    

        /// <summary>
        /// 主数据库连接
        /// </summary>
        public string PrimaryDBConnectionString
        {
            get
            {
                return this.getDBConnectionString("PrimaryDBConnectionString");
            }
            set
            {
                this.setDBConnectionString("PrimaryDBConnectionString", value);
            }
        }


        /// <summary>
        /// 备用数据库连接字符串
        /// </summary>
        public string StandbyDBConnectionString
        {
            get
            {
                return this.getDBConnectionString("StandbyDBConnectionString");
            }
            set
            {
                this.setDBConnectionString("StandbyDBConnectionString", value);
            }
        }


        /// <summary>
        /// 获取数据库连接字符串
        /// </summary>
        /// <param name="elementName"></param>
        /// <returns></returns>
        private string getDBConnectionString(string elementName)
        {
            string result = string.Empty;
            XmlElement element = base.GetElement(elementName, true);
            switch (this.DBType)
            {
                case DBType.SqlServer:
                    result = new SqlServerConnectionStringBuilder
                    {
                        ConnectionString = element.InnerText,
                        Password = element.GetAttribute("Password").DecryptString()
                    }.ConnectionString;
                    break;
                case DBType.MySql:
                    result = element.InnerText;
                    break;
                case DBType.Oracle:
                    result = element.InnerText;
                    break;
                case DBType.PostgreSql:
                    result = element.InnerText;
                    break;
            }
            return result;
        }

        // Token: 0x060000A1 RID: 161 RVA: 0x00003114 File Offset: 0x00001314
        private void setDBConnectionString(string elementName, string connectionString)
        {
            XmlElement element = base.GetElement(elementName, true);
            switch (this.DBType)
            {
                case DBType.SqlServer:
                    {
                        SqlServerConnectionStringBuilder msSqlConnectionStringBuilder = new SqlServerConnectionStringBuilder();
                        msSqlConnectionStringBuilder.ConnectionString = connectionString;
                        element.SetAttribute("Password", msSqlConnectionStringBuilder.Password.EncryptString());
                        msSqlConnectionStringBuilder.Password = string.Empty;
                        element.InnerText = msSqlConnectionStringBuilder.ConnectionString;
                        return;
                    }
                case DBType.MySql:
                    element.InnerText = connectionString;
                    return;
                case DBType.Oracle:
                    element.InnerText = connectionString;
                    return;
                case DBType.PostgreSql:
                    element.InnerText = connectionString;
                    return;
                default:
                    return;
            }
        }
    }
}
